<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Contact extends MX_Controller {
	function __construct() {
		parent::__construct ();

		$_SESSION ['KCFINDER'] = array ();
		$_SESSION ['KCFINDER'] ['disabled'] = false;

		$this->load->library ( 'ckeditor', array ('instanceName' => 'CKEDITOR1', 'basePath' => base_url () . "ckeditor/", 'outPut' => true ) );
		if (!isset($this->session->userdata['language'])){
			$this->session->userdata['language']='';
			$this->session->set_userdata('data_language', '');
		}
		$this->lang->load('system', $this->session->userdata['language']);
		$this->load->model ( 'contact_model' );
		$this->load->model ( '../../about/models/about_model' );
	}

	function index(){
		$data ['company'] = $this->about_model->getAboutDetail();
		$data ['content'] = 'default';
		$this->load->view ( 'front_end/index', $data );
	}
	function save(){
		if ($this->input->post ()) {
			$data = array ();
			$data ['name'] = ( string ) $this->input->post ( 'name' );
			$data ['email'] = ( string ) $this->input->post ( 'email' );
			$data ['phone'] = ( string ) $this->input->post ( 'phone' );
			$data ['address'] = ( string ) $this->input->post ( 'address' );
			$data ['message'] = ( string ) $this->input->post ( 'message' );
			$this->contact_model->saveItem ( 'contact', array ('field' => 'id', 'id' => 0 ), $data );

			$email_content = 'Họ và tên : '.$this->input->post ( 'name' )."<br>";
			$email_content .= 'Email  : '.$this->input->post ( 'email' )."<br>";
			$email_content .= 'Số điện thoại : '.$this->input->post ( 'phone' )."<br>";
			$email_content .= 'Địa chỉ : '.$this->input->post ( 'address' )."<br>";
			$email_content .= '<p> Họ và tên : '.$this->input->post ( 'message' )."</p>";
			$status=$this->sendContactUs('nguyenduydinhdl@gmail.com','New contact from TMS',$email_content);
			$data ['company'] = $this->about_model->getAboutDetail();
			$data['success']=$status;
			$data ['content'] = 'default';
			$this->load->view ( 'front_end/index', $data );
		}
	}

	public function sendContactUs($to, $subject, $content){
		$this->load->library('email');
		$config['protocol'] = "smtp";
		$config['smtp_host'] = "ssl://smtp.gmail.com";
		$config['smtp_user'] = "dinhnd.dands@gmail.com";
		$config['smtp_pass'] = "******";
		$config['smtp_port'] = "465";

		$config['newline'] = "\r\n";
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$config['mailtype'] = 'html';

		$this->email->initialize($config);
		$this->email->from('dinhnd.dands@gmail.com', 'TMS SYSTEM');
		$this->email->to($to);
		$this->email->subject($subject);
		$this->email->message($content);
		return $this->email->send();
	}

	function register(){
		if ($this->input->post ()) {
			$data = array ();
			$data ['name'] = ( string ) $this->input->post ( 'name' );
			$day= ( string ) $this->input->post ( 'day' );
			$month= ( string ) $this->input->post ( 'month' );
			$year= ( string ) $this->input->post ( 'year' );

			$data ['birthday'] = ( string ) "$day/$month/$year";
			$data ['email'] = ( string ) $this->input->post ( 'email' );
			$data ['phone'] = ( string ) $this->input->post ( 'phone' );
			$data ['address'] = ( string ) $this->input->post ( 'address' );
			$data ['message'] = ( string ) $this->input->post ( 'message' );
			$data ['education'] = ( string ) $this->input->post ( 'education' );
			$status=$this->contact_model->saveItem ( 'register', array ('field' => 'id', 'id' => 0 ), $data );
			$data['success']=$status;
			$data ['content'] = 'register';
			$this->load->view ( 'front_end/index', $data );
		}
		$data ['content'] = 'register';
		$this->load->view ( 'front_end/index', $data );
	}
}